/*
 * Copyright 2011 J. Auer, J. Andert, F.-J. Jendrusch, U. Celebi
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package de.fuberlin.replication.library;

import java.io.Serializable;
import java.util.Vector;

import de.fuberlin.replication.storage.MetadataDAO;

/**
 * Auxiliary class representing an Subset (ID, knowledge vector, objects in this subset).
 */
public class Subset implements Serializable {
	private static final long serialVersionUID = 4636838423751798358L;

	/**
	 * Unique identifier for this subset.
	 */
	private long id;

	/**
	 * Knowledge vector of this subset.
	 */
	private VersionVector knowledge;

	/**
	 * List of object names suspected to be in this subset.
	 */
	private Vector<String> objectNames;

	/**
	 * Constructor.
	 */
	public Subset() {
		knowledge = new VersionVector();
		objectNames = new Vector<String>();
	}

	/**
	 * Constructor for initialing Subset with knowledge vector.
	 * 
	 * @param knowledge Knowledge vector of this subset
	 */
	public Subset(VersionVector knowledge) {
		this.knowledge = knowledge;
		objectNames = new Vector<String>();
	}

	@Override
	public String toString() {
		return "[" + knowledge.toString() + ";" + objectNames.toString() + "]";
	}

	/**
	 * Get the unique id of this subset.
	 * 
	 * @return Unique id
	 */
	public long getId() {
		return id;
	}

	/**
	 * Set the unique id of this subset.
	 * 
	 * @param id Unique id
	 */
	public void setId(long id) {
		this.id = id;
	}

	/**
	 * Set the knowledge vector of this subset for given knowledge vector.
	 * 
	 * @param knowledge New knowledge vector
	 */
	public void setKnowledge(VersionVector knowledge) {
		this.knowledge = knowledge;
	}

	/**
	 * Get the knowledge vector of this subset.
	 * 
	 * @return The knowledge vector of this subset
	 */
	public VersionVector getKnowledge() {
		return knowledge;
	}

	/**
	 * Get a list of Strings, representing the names of all objects in this subset. The list is never created
	 * automatically and may not represent the persistent state of an subset.
	 * 
	 * @return A list of names of all objects in this Subset
	 */
	public Vector<String> getObjectNames() {
		return objectNames;
	}

	/**
	 * Set the list of objects in this subset.
	 * 
	 * @param names A list of object names
	 */
	public void setObjectNames(Vector<String> names) {
		this.objectNames = names;
	}

	/**
	 * Find all object names of objects that currently are in this subset, using a given storage implementation.
	 * 
	 * @param metadataDAO MetadataDAO of a given Storage implementation
	 * @return A list of object names
	 */
	public Vector<String> findObjectNames(MetadataDAO metadataDAO) {
		Vector<String> names = new Vector<String>();

		for (Metadata object : metadataDAO.getAll(this))
			names.add(object.getName());

		return names;
	}

	/**
	 * Add a name to the list of object names associated to this Subset.
	 * 
	 * @param name Object name
	 */
	public void addObjectName(String name) {
		if (!objectNames.contains(name))
			objectNames.add(name);
	}

	/**
	 * Remove a name from the list of object names associated to this subset.
	 * 
	 * @param name Object name
	 */
	public void removeObjectName(String name) {
		objectNames.remove(name);
	}

	/**
	 * Check if an objects name occurs in this subset's list of object names.
	 * 
	 * @param name Object name to search for
	 * @return True if the name is known, false otherwise
	 */
	public boolean containsObject(String name) {
		for (String n : objectNames)
			if (n.equals(name))
				return true;

		return false;
	}

	/**
	 * Check if the given version is known to this subset. A version is known, if a version with the same replica
	 * identifier and a version number <= the version number of the given version exists in the subsets knowledge
	 * vector.
	 * 
	 * @param version A given version
	 * @return true if the version is known, false otherwise
	 */
	public boolean containsVersion(Version version) {
		for (Version v : knowledge)
			if (version.getReplica().equals(v.getReplica()) && version.getVersion() <= v.getVersion())
				return true;

		return false;
	}
}